package com.hyzx.qbasic.admin.common.util;

import com.hyzx.qbasic.admin.model.constant.CacheKeyConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

/**
 * Created by 黄江华 on 2017/9/20.
 */

@Component
public class LocalCacheUtils {

    private static CacheManager cacheManager;

    @Autowired
    public void setCacheManager(CacheManager cacheManager) {
        LocalCacheUtils.cacheManager = cacheManager;
    }

    public static <T> void set(String key, T value) {
        set(CacheKeyConst.DEFAULT_CACHE_NAME, key, value);
    }

    public static <T> void set(String cacheName, String key, T value) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.put(key, value);
    }

    public static <T> T get(String key, Class<T> clazz) {
        return get(CacheKeyConst.DEFAULT_CACHE_NAME, key, clazz);
    }

    public static <T> T get(String cacheName, String key, Class<T> clazz) {
        Cache cache = cacheManager.getCache(cacheName);
        return cache.get(key, clazz);
    }
}
